/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_http_analyser.h
* @version     
* @brief      
* @author   
* @date     2013-04-27
* @note 
*
*  1. 2018-04-27 created this file
* 
*/
#pragma once

#include <duye_cap_http_parser.h>
#include <duye_cap_tcp_session.h>

namespace duye {

class HttpAnalyserResult {
public:
	typedef std::pair<uint8* /* headerField */, uint8* /* headerValue */> HeaderPair;
	typedef std::list<HeaderPair> HeaderPairList;
	typedef struct tag_Body {
		uint8* data;
		int32 len;
	} Body;

public:
	HttpAnalyserResult();
	~HttpAnalyserResult();

	void clear();

public:
	bool m_isComplete;
	uint8* m_url;
	uint8* m_status;
	Body m_body;
	HeaderPairList m_headerPairList;
};

class HttpAnalyser : public HttpParserListener {
public:
	HttpAnalyser();
	~HttpAnalyser();

	int32 analyse(const PacketReference& pkt, HttpAnalyserResult& result);
	int32 analyse(const PacketReference& pkt, const TcpSession& session, HttpAnalyserResult& result);

private:
	// inherit from HttpParserListener
	virtual int32 onMessageBegin(HttpParserData* data);
	virtual int32 onUrl(HttpParserData* data, const uint8* url, int32 len);
	virtual int32 onStatus(HttpParserData* data, const uint8* status, int32 len);
	virtual int32 onHeaderField(HttpParserData* data, const uint8* headerField, int32 len);
	virtual int32 onHeaderValue(HttpParserData* data, const uint8* headerValue, int32 len);
	virtual int32 onHeadersComplete(HttpParserData* data);
	virtual int32 onBody(HttpParserData* data, const uint8* body, int32 len);
	virtual int32 onMessageComplete(HttpParserData* data);

	void checkForReset();

private:
	HttpParser m_parser;
	bool m_needReset;

	HttpAnalyserResult m_result;
	HttpAnalyserResult::HeaderPair m_tmpPair;
};

}
